/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
    if (x <= 1) return x
    let start = 0,
        end = x,
        mid,
        ans = -1
    while (start <= end) {
        mid = ~~((start + end) / 2)

        if (mid * mid === x) {
            return mid
        } else if (mid * mid > x) {
            end = mid - 1
        } else {
            ans = mid
            start = mid + 1
        }
    }

    return ans
};